package com.yuwenchao.superafflatus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuwenchao.superafflatus.constant.AfflatusConstant;
import com.yuwenchao.superafflatus.mapper.SuperLoginUserMapper;
import com.yuwenchao.superafflatus.pojo.entity.SuperLoginUser;
import com.yuwenchao.superafflatus.service.SuperLoginRoleUserService;
import com.yuwenchao.superafflatus.service.SuperLoginUserService;
import com.yuwenchao.superafflatus.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ChaooGG
 * @since 2021-05-31
 */
@Api(tags = "用户登录信息")
@RestController
@RequestMapping("/superLoginUser")
@Slf4j
public class SuperLoginUserController {


    @Autowired
    SuperLoginUserMapper superLoginUserMapper;


    @Autowired
    SuperLoginUserService superLoginUserService;

    @Autowired
    SuperLoginRoleUserService superLoginRoleUserService;

    @Autowired
    private BCryptPasswordEncoder encoding;

    @ApiOperation("监听用户是否存在")
    @PostMapping("checkLoginacct")
    public SuperLoginUser checkLoginacct(String username) {
        QueryWrapper<SuperLoginUser> loginUserQueryWrapper = new QueryWrapper<>();
        SuperLoginUser superLoginUser = superLoginUserMapper.selectOne(loginUserQueryWrapper.eq("username", username));
        return superLoginUser;
    }


    @ApiOperation("注册功能")
    @PostMapping("register")
    public R registerUser(SuperLoginUser user) {
        log.info("用户信息"+user);
        if(user!=null){
            String secPassword = encoding.encode(user.getPassword());
            user.setPassword(secPassword);
            user.setAvatar(AfflatusConstant.DEFAULT_AVATAR);
            user.setLetter(AfflatusConstant.DEFAULT_LETTER);
            int insert = superLoginUserMapper.insert(user);
            //注册成功或应该处理用户角色表，将角色改为普通用户，这里手动处理

            String username = user.getUsername();
            QueryWrapper<SuperLoginUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username",username);
            SuperLoginUser superLoginUser = superLoginUserMapper.selectOne(queryWrapper);
            Integer id = superLoginUser.getId();
            superLoginRoleUserService.saveRoleUser(id);
            System.out.println("新注册用户授权成功！");

            return R.ok().data("user",user);
        }

        return  R.error();
    }



    @ApiOperation("查询所有用户名和id")
    @PostMapping("getUserName")
    public List<SuperLoginUser> getUserName() {
        QueryWrapper<SuperLoginUser> loginUserQueryWrapper = new QueryWrapper<>();
        loginUserQueryWrapper.select("id", "username");
        List<SuperLoginUser> superLoginUsers = superLoginUserMapper.selectList(loginUserQueryWrapper);




        //这里主要用于测试学习用jackson包实现json、对象、Map之间的转换
        // https://www.cnblogs.com/love-cj/p/8242382.html
      /*  try {
            ObjectMapper mapper = new ObjectMapper(); //转换器
            String json=mapper.writeValueAsString(superLoginUsers.get(0));////将对象转换成json
            System.out.println("对象转json"+json);

            Map m = mapper.readValue(json, Map.class); //json转换成map
           // m.put("disabled",true);
            System.out.println("json转换成map"+m);


            json=mapper.writeValueAsString(m); //map转json
            System.out.println("map转json "+json);

            SuperLoginUser superLoginUser=mapper.readValue(json, SuperLoginUser.class); //json转java对象。
            System.out.println("json转对象 "+json);


        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
*/

        return superLoginUsers;
    }

    //查出是管理员用户的id
    @ApiOperation("查出是管理员用户的id")
    @PostMapping("getRoleUserId")
    public List<Integer> getRoleUserById() {

        List<Integer> roleUserIdList  = superLoginUserService.getRoleUserId();
        return roleUserIdList;
    }


    @ApiOperation("根据id修改用户信息")
    @PostMapping("getUserInfoById/{id}")
    public R getUserInfoById(@PathVariable Integer id, @RequestBody SuperLoginUser loginuser) {
        superLoginUserMapper.updateById(loginuser);
        return R.ok();
    }

    @ApiOperation("根据id修改用户密码")
    @PostMapping("setPasswordById")
    public R setPasswordById(@RequestParam("id") Integer id,@RequestParam("oldPassword") String oldPassword,@RequestParam("password") String password) {
        SuperLoginUser superLoginUser = superLoginUserMapper.selectById(id);
        String DBpassword = superLoginUser.getPassword();

        System.out.println("原密码为"+DBpassword);
        System.out.println("输入的原密码为"+oldPassword);
        //新密码进行加密
       //这里需要注意只能用解密 不能用加密，因为每次加密后的密码不一样
        if(encoding.matches(oldPassword,DBpassword)){
            //根据id修改password
            superLoginUserService.setPasswordById(id,encoding.encode(password));
            return R.ok().message("密码修改成功！");
        }else{
            return R.error().message("原密码不正确！");
        }
    }

}

